package com.hk.devp.captcha.web;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.apiclub.captcha.Captcha;
import cn.apiclub.captcha.audio.AudioCaptcha;
import cn.apiclub.captcha.backgrounds.GradiatedBackgroundProducer;
import cn.apiclub.captcha.servlet.CaptchaServletUtil;
import cn.apiclub.captcha.text.renderer.ColoredEdgesWordRenderer;

/**
* @ClassName: SimpleCaptchaController 
* @author: dragon0574@sina.com
* @date 2017年7月31日 上午9:46:24  
* @Description: SimpleCatpcha开源验证码框架的Controller包含音频和图片
* 
 */
@RequestMapping("/simpleCaptcha")
@Controller
public class SimpleCaptchaController {
	
	@RequestMapping("/toSimpleCaptchaPage")
	public String toSimpleCaptchapPage(){
		
		return "simpleCapthca";
	}
	
	@RequestMapping("/audio.wav")
	public void getAudioCaptcha(HttpServletRequest request,HttpServletResponse response){
		AudioCaptcha ac = new AudioCaptcha.Builder()
	            .addAnswer()
	            .addNoise()
	            .build();

		request.getSession().setAttribute(AudioCaptcha.NAME, ac);
	        CaptchaServletUtil.writeAudio(response, ac.getChallenge());
		
	}
	
	
	@RequestMapping("/simpleCaptcha.png")
	public void getSimpleCaptcha(HttpServletRequest request,HttpServletResponse response){
		int _width = 250;
	    int _height = 75;
	    
	    List<Color> COLORS = new ArrayList<Color>(2);
	    List<Font> FONTS = new ArrayList<Font>(3);
	    
        COLORS.add(Color.BLACK);
        COLORS.add(Color.BLUE);

        FONTS.add(new Font("Geneva", Font.ITALIC, 48));
        FONTS.add(new Font("Courier", Font.BOLD, 48));
        FONTS.add(new Font("Arial", Font.BOLD, 48));
	
		
		 ColoredEdgesWordRenderer wordRenderer = new ColoredEdgesWordRenderer(COLORS, FONTS);
        Captcha captcha = new Captcha.Builder(_width, _height).addText(wordRenderer)
                .gimp()
                .addNoise()
                .addBackground(new GradiatedBackgroundProducer())
                .build();

        CaptchaServletUtil.writeImage(response, captcha.getImage());
        request.getSession().setAttribute(Captcha.NAME, captcha);
		
	}
	
	@RequestMapping("/stickyCaptcha.png")
	public void getStickyCaptcha(HttpServletRequest request,HttpServletResponse response){
		int _width = 250;
	    int _height = 75;
	   // long _ttl = 1000 * 60 * 10;
	    long _ttl = 100000;
	    
	    HttpSession session = request.getSession();
        Captcha captcha;

        if (session.getAttribute(Captcha.NAME) == null) {
            captcha =new Captcha.Builder(_width, _height)
                    .addText()
                    .gimp()
                    .addBorder()
                    .addNoise()
                    .addBackground()
                    .build();

                session.setAttribute(Captcha.NAME, captcha);
        }

        captcha = (Captcha) session.getAttribute(Captcha.NAME);
        if (shouldExpire(captcha,_ttl)) {
        	 captcha =new Captcha.Builder(_width, _height)
                     .addText()
                     .gimp()
                     .addBorder()
                     .addNoise()
                     .addBackground()
                     .build();
                 session.setAttribute(Captcha.NAME, captcha);
        }
        CaptchaServletUtil.writeImage(response, captcha.getImage());
	}
	
	@RequestMapping("/login")
	public String simpleCaptchaLogin(){
		
		return "";
	}
	
	
	 static boolean shouldExpire(Captcha captcha,long _ttl) {
	        long ts = captcha.getTimeStamp().getTime();
	        long now = new Date().getTime();
	        long diff = now - ts;

	        return diff >= _ttl;
	    }
	
	
	
}
